jQuery(document).ready(function($) {
    getWonderfulArticle();
    getAllSection();
    getCarousel();
    getserInfoBySession();
    
  //进入文章详情
  	$(document).on('click', '.list-group-item>a', function() {
  		var articleId = $(this).attr('data-bind');
  		 window.open(getContextPath()+'/article/showarticle?articleId='+articleId);
  	});
});


/**
 * 获取用户信息并初始赋值
 */
function getserInfoBySession(){
	var getserinfobysessionURL = getContextPath()+"/user/getserinfobysession";
	$.get(getserinfobysessionURL, function(data) {
		console.log(data);
		var obj=eval("("+data+")"); //转换json
		if(obj.sucess == true){
		var user = obj.user;
		$('#user_login').html('<center><a href="/smm3/user/backStag"><img style="width:50px;height:50px;border-radius:50px;" src="/file/image/'+user.userImageUrl+'"><a/></center>')
		$('#user_logout').html('')
		}else{
			console.log('获取用户信息失败');
		}
	});
}


/**
 * 搜索按钮 搜索并跳转
 */
function serachClick(){
	var articleName = $('.form-control').val();
	window.location.href="articlesort.jsp?articleName="+articleName;

}


/**
 * 获取轮播图的文章 即是是否推荐
 */
function getCarousel(){
	var getWonderfulArticleURL=getContextPath()+"/article/getcarousel";
    $.get(getWonderfulArticleURL, function(data) {
        console.log('轮播图数据'+data);
        $('.carousel-inner').html('');
        $('.carousel-indicators').html('');
        var obj=eval("("+data+")");
        var article =obj.article;
        var temphtml1="1";
        var temphtml2="2";
        $.map(article, function(item, index) {
        	if(index==0){
            temphtml1='<li data-target="#myCarousel" data-slide-to="'+index+'" class="active"></li>';
        	}else{
        	temphtml1='<li data-target="#myCarousel" data-slide-to="'+index+'"></li>';	
        	}
        	$('.carousel-indicators').append(temphtml1);
        	if(index==0){
        	temphtml2='<div class="item active"><a target="_blank" href="/smm3/article/showarticle?articleId='+item.articleId+'"><img src="/file/image/'+item.articleCover+'" date-bind="'+item.articleId+'" style="width:490px;height:290px" alt="'+item.articleName+'"></a><div class="carousel-caption">'+item.articleName+'</div></div>';
        	}else{
        		temphtml2='<div class="item"><a target="_blank" href="/smm3/article/showarticle?articleId='+item.articleId+'"><img src="/file/image/'+item.articleCover+'" style="width:490px;height:290px" alt="'+item.articleName+'"><a/><div class="carousel-caption">'+item.articleName+'</div></div>';
        	}
        	$('.carousel-inner').append(temphtml2)
        });
    });
	
}


/**
 * 获取文章排行
 */
function getWonderfulArticle(){
	
	var getWonderfulArticleURL=getContextPath()+"/article/getwonderfularticle";
	$.post(getWonderfulArticleURL, {'currentPage': 1,'onePageNum':5}, function(data, textStatus, xhr) {
		/*optional stuff to do after success */
		console.log(data);
		var obj=eval("("+data+")");
		var article = obj.article; //article 列表
		$('.list-group').html('');
		for (var i = 0; i < article.length; i++) {
            var articleName = article[i].articleName.replace(/<[^>]+>/g,"");
            var articleTime =getLocalTime(article[i].articleTime) ;
            if(articleName.length>20){
                articleName = articleName.substr(0,20)+'...';
            }
			var temphtml = '<li class="list-group-item"><img src="" alt=""><span class="badge">'+articleTime+'</span>'+'<a href="javascript:;" data-bind="'+article[i].articleId+'">'+articleName+'</li>'+'</a>'
			$('.list-group').append(temphtml);
		}
	});
}

/**
 * 获取所有板块
 */
function getAllSection(){
    var getAllSectionURL = getContextPath()+"/front/getallsection";

    $.ajax({
        url: getAllSectionURL,
        async : false, //设置成为 同步的 ,
    })
        .done(function(data) {
            console.log("success");
            var obj=eval("("+data+")"); //转换json
            //alert(obj.sucess==true);
            if(obj.sucess==true){
                var section = obj.section;
                $.map(section, function(item, index) {
                    if((index+1)%3==0){
                        var  tempHTML='<a class="btn btn-default" target="_black" href="articlesort.jsp?sectionId='+item.sectionId+'" role="button">'+item.sectionName+'</a><br>';
                    }else {
                    var  tempHTML='<a class="btn btn-default" target="_black" href="articlesort.jsp?sectionId='+item.sectionId+'" role="button">'+item.sectionName+'</a>'
                    }
                    $('.section').append(tempHTML);
                });
            }
            console.log(data);
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });

}




/*
 * java Date格式 转化
 */
function dateFtt(fmt,date)   
{ //author: meizz   
  var o = {   
    "M+" : date.getMonth()+1,                 //月份   
    "d+" : date.getDate(),                    //日   
    "h+" : date.getHours(),                   //小时   
    "m+" : date.getMinutes(),                 //分   
    "s+" : date.getSeconds(),                 //秒   
    "q+" : Math.floor((date.getMonth()+3)/3), //季度   
    "S"  : date.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
}

function getLocalTime(nS) {  
	var crtTime = new Date(nS);
	 return top.dateFtt("yyyy-MM-dd",crtTime);
}


function getContextPath() { //获取根路径

	var pathName = document.location.pathname;
	var index = pathName.substr(1).indexOf("/");
	var result = pathName.substr(0, index + 1);
	return result;
}